package com.spring.campus_express_delivery.controller;

import com.github.pagehelper.PageInfo;
import com.spring.campus_express_delivery.entity.Notices;
import com.spring.campus_express_delivery.entity.Users;
import com.spring.campus_express_delivery.interceptor.RequiredPermission;
import com.spring.campus_express_delivery.service.NoticesService;
import com.spring.campus_express_delivery.service.UsersService;
import com.spring.campus_express_delivery.util.JwtToken;
import com.spring.campus_express_delivery.util.MyUtil;
import com.spring.campus_express_delivery.util.RedisUtil;
import com.spring.campus_express_delivery.util.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/notices")
@Api(value = "NoticesController", description = "公告相关api")
public class NoticesController {
    Boolean flag;
    MyUtil myUtil=new MyUtil();

    @Resource
    NoticesService noticesService;

    @Resource
    private UsersService usersService;
    ResponseEntity responseEntity ;
    @Autowired
    RedisUtil redisUtil;


    /**
     * 添加公告
     * @param noticeNum
     * @param noticeTitle
     * @param noticeContent
     * @return
     */
    @ApiOperation("添加公告")
    @PostMapping("/web/add_notice")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity insertNotices(@RequestParam(required = true) String noticeNum,
                                        @RequestParam(required = false) String noticeTitle,
                                        @RequestParam(required = false) String noticeContent,
                                        @RequestHeader String Authorization) throws IOException {
        String token = Authorization;
            responseEntity = ResponseEntity.ok();
            Users users = JwtToken.unsign(token, Users.class);
            Notices notices1=noticesService.selectId(noticeNum);
        if (notices1 == null) {
            Notices notices = new Notices();
            notices.setNoticeNum(noticeNum);
            notices.setNoticeTitle(noticeTitle);
            notices.setNoticeContent(noticeContent);
            notices.setNoticeTime(myUtil.getTime());
            noticesService.insertNotices(notices);
            responseEntity.putDataValue("msg", "通知添加成功");
            return responseEntity;
        } else{
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","公告已存在不可添加");
            return responseEntity;
        }

    }

    /**
     * 公告列表
     * @return
     */
    @ApiOperation("公告列表")
    @GetMapping("/web/notice_list")
    @ResponseBody
    public ResponseEntity selectAll(@RequestParam(value="pageNo",defaultValue="1")int pageNo,
                                    @RequestParam(value="pageSize",defaultValue="10")int pageSize){
        responseEntity = ResponseEntity.ok();
        PageInfo<Notices> list=noticesService.selectAll(pageNo,pageSize);
        responseEntity.putDataValue("list",list);
        return responseEntity;

    }

    /**
     * 单个公告查询
     * @param noticeNum
     * @return
     */
    @ApiOperation("单个公告查询")
    @GetMapping("/web/select_notice_id")
    @ResponseBody
    public ResponseEntity selectId(@RequestParam(required = true) String noticeNum) {
        //查询缓存
        String key="notices_"+noticeNum;
        boolean b = redisUtil.hasKey(key);
        //如果redis中存在 查询缓存
        if (b){
            Notices notices = (Notices) redisUtil.get(key);
            responseEntity=ResponseEntity.ok();
            responseEntity.putDataValue("notices",notices);
            return responseEntity;
        }
        responseEntity = ResponseEntity.ok();
        Notices notices = noticesService.selectId(noticeNum);
        responseEntity.putDataValue("selectdata",notices);
        redisUtil.set("notices_" + noticeNum, notices);
        return responseEntity;
    }

    /**
     * 修改公告
     * @param noticeNum
     * @param noticeTitle
     * @param noticeContent
     * @return
     */
    @ApiOperation("修改公告")
    @PostMapping ("/web/update_notice")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity updateId( @RequestParam(required = true) String noticeNum,
                                    @RequestParam(required = false) String noticeTitle,
                                    @RequestParam(required = false) String noticeContent,
                                    @RequestHeader String Authorization) throws IOException {
        String token = Authorization;
            responseEntity = ResponseEntity.ok();
            Users users = JwtToken.unsign(token, Users.class);
            Notices notices1=noticesService.selectId(noticeNum);
            if(notices1!=null){
                    Notices notices=new Notices();
                    notices.setNoticeNum(noticeNum);
                    notices.setNoticeTitle(noticeTitle);
                    notices.setNoticeContent(noticeContent);
                    notices.setNoticeTime(myUtil.getTime());
                    noticesService.updateId(notices);
                    responseEntity.putDataValue("msg","修改成功");
                    //删除缓存
                    String key = "notices_" + noticeNum;
                    if (redisUtil.hasKey(key)){
                        redisUtil.del(key);
                    }
            }else{
                responseEntity=ResponseEntity.forbidden();
                responseEntity.putDataValue("msg","该公告不存在，修改失败");
                return responseEntity;
            }
        return responseEntity;
    }

    /**
     * 删除公告
     * @param noticeNum
     * @return
     */
    @ApiOperation("删除公告")
    @DeleteMapping("/web/delete_notice")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity deleteId(@RequestParam(required = true) String noticeNum,@RequestHeader String Authorization) throws IOException {
        String token = Authorization;
        ResponseEntity responseEntity;
        Users users = JwtToken.unsign(token, Users.class);
        Notices notices1=noticesService.selectId(noticeNum);
        if (notices1!=null){
            noticesService.deleteId(noticeNum);
            responseEntity = ResponseEntity.ok();
            responseEntity.putDataValue("msg","删除成功");
            //删除缓存
            String key = "notices_" + noticeNum;
            if (redisUtil.hasKey(key)){
                redisUtil.del(key);
            }
            return responseEntity;
        }else{
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","公告不存在，删除失败");
            return responseEntity;
        }

    }
    /**
     * 导入公告信息
     * @param userFiles
     * @Header Authorization
     * @thorws IOException
     */
    @ApiOperation("导入公告信息api")
    @PostMapping("web/importNoticesExcel")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity importNoticesExcel(@RequestParam MultipartFile userFiles,@RequestHeader String Authorization) throws IOException{
        noticesService.importNoticesExcel(userFiles);
        ResponseEntity responseEntity;
        responseEntity=ResponseEntity.ok();
        responseEntity.putDataValue("msg","导入公告信息成功");
        return responseEntity;//那个是impl文件
    }
    /**
     * poi将数据库中的用户数据批量导出到Excel
     * @param response
     * @Header Authorization
     * @throws Exception
     */
    @ApiOperation("导出公告信息api")
    @PostMapping("web/exportNoticesExcel")
    @ResponseBody
    @RequiredPermission(1)
    public void exportNoticesExcel(HttpServletResponse response,@RequestHeader String Authorization) throws Exception {
        noticesService.exportNoticesExcel(response);
    }
    /**
     * 根据公告编号或者公告标题或者公告内容模糊查询公告
     *
     */
    @ApiOperation("根据公告编号或者公告标题或者公告内容模糊查询公告")
    @GetMapping("/web/select_notice")
    @ResponseBody
    public ResponseEntity selectNotice(@RequestParam(required = true) String keyWord) {
        responseEntity = ResponseEntity.ok();
        responseEntity.putDataValue("selectdata",noticesService.selectNotice(keyWord));
        return responseEntity;
    }


}
